<?php
/**
 * Created by PhpStorm.
 * User: tjjs
 * Date: 17-7-19
 * Time: 下午9:36
 */

namespace Utils;

use Data\Food_DAO;
use Data\Order_for_foods_DAO;
use Data\Order_form_DAO;
use Data\Restaurant_DAO;
use Data\Statistics_restaurant_day_turnover;
use Data\Statistics_restaurant_day_turnover_DAO;

class StatisticsRestaurant
{
    public function statisticsRestaurantDayTurnover() {
        // 今天零点时间戳
        $today_time = strtotime(date('Ymd'));
        // 昨天零点时间戳
        $yesterday_time = $today_time-86400;
        // 昨天日期
        $yesterday_date = date("Y-m-d",$yesterday_time);
        // 前天零点时间戳
        $last_day_time = $yesterday_time-86400;
        // 前天日期
        $last_day_date = date("Y-m-d",$last_day_time);

        // 统计昨天营业

        // 商铺列表
        $restaurant_id_list = Restaurant_DAO::getRestuarantIdList();
        foreach ($restaurant_id_list as $restaurant_id) {
            // 今日营业
            $form_record = Order_form_DAO::getSumTotalPrice($restaurant_id,$yesterday_date);
            // 营业额
            $turnover = $form_record[TURNOVER];
            // 订单数量
            $total_food_count = $form_record[TOTAL_FOOD_COUNT];
            // 顾客数量
            $cusumer_count = $form_record[CUSUMOR_COUNT];

            // 昨日营业
            $form_record_yesterday = Order_form_DAO::getSumTotalPrice($restaurant_id,$last_day_date);
            // 昨日营业额
            $turnover_yesterday = $form_record_yesterday[TURNOVER];
            // 昨日订单数量
            $total_food_count_yesterday = $form_record_yesterday[TOTAL_FOOD_COUNT];
            // 昨日顾客数量
            $cusumer_count_yesterday = $form_record_yesterday[CUSUMOR_COUNT];


            // 较昨日营业额
            $turnover_compare = $turnover-$turnover_yesterday;
            // 较昨日订单数量
            $total_food_count_compare = $total_food_count-$total_food_count_yesterday;
            // 较昨日顾客数量
            $cusumer_count_compare = $cusumer_count-$cusumer_count_yesterday;

            // 每种商品销量统计
            $order_for_foods_list = Order_for_foods_DAO::getFoodsList($restaurant_id,$yesterday_date);

            // 销量排序
            $food_saled_count = array();
            foreach ($order_for_foods_list['order_by_count'] as $order_for_foods_record) {
                $food_id = $order_for_foods_record[FOOD_ID];
                $food_name = Food_DAO::getName($food_id);
                $order_for_foods_record[FOOD_NAME] = $food_name;
                $food_saled_count[] = $order_for_foods_record;
            }

            $food_saled_count_json = addslashes(json_encode($food_saled_count));


            // 销售额排序
            $food_saled_price = array();
            foreach ($order_for_foods_list['order_by_price'] as $order_for_foods_record) {
                $food_id = $order_for_foods_record[FOOD_ID];
                $food_name = Food_DAO::getName($food_id);
                $order_for_foods_record[FOOD_NAME] = $food_name;
                $food_saled_price[] = $order_for_foods_record;
            }


            $food_saled_price_json = addslashes(json_encode($food_saled_price));

            $statistics_restaurant_day_turnover_record = new Statistics_restaurant_day_turnover(array());
            $statistics_restaurant_day_turnover_record->setDATE($yesterday_date);
            $statistics_restaurant_day_turnover_record->setRESTAURANT_ID($restaurant_id);
            $statistics_restaurant_day_turnover_record->setTURNOVER($turnover);
            $statistics_restaurant_day_turnover_record->setTURNOVER_COMPARE($turnover_compare);
            $statistics_restaurant_day_turnover_record->setTOTAL_FOOD_COUNT($total_food_count);
            $statistics_restaurant_day_turnover_record->setTOTAL_FOOD_COUNT_COMPARE($total_food_count_compare);
            $statistics_restaurant_day_turnover_record->setCUSUMOR_COUNT($cusumer_count);
            $statistics_restaurant_day_turnover_record->setCUSUMOR_COUNT_COMPARE($cusumer_count_compare);
            $statistics_restaurant_day_turnover_record->setFOOD_SALED_COUNT($food_saled_count_json);
            $statistics_restaurant_day_turnover_record->setFOOD_SALED_PRICE($food_saled_price_json);

//            var_dump($statistics_restaurant_day_turnover_record);
            Statistics_restaurant_day_turnover_DAO::insert($statistics_restaurant_day_turnover_record);
        }
    }
}